package com.house.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.house.common.Result;
import com.house.entity.Message;
import com.house.service.MessageService;
import com.house.util.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 消息管理
 */
@Controller
@RequestMapping("message")
public class MessageController {


    @Autowired
    MessageService messageService;


    /**
     * 租赁信息管理页
     * @return
     */
    @GetMapping("manage")
    public String manage() {
        return "admin/messageManage";
    }

    @PostMapping("manage/list")
    @ResponseBody
    public List<Message> list(Message message) {
        return messageService.list(Wrappers.<Message>query().orderByDesc("create_time"));
    }



    /**
     * 查询消息提醒
     * @return
     */
    @PostMapping("remind")
    @ResponseBody
    public Result remind() {
        // 消息提醒 只提醒最后一条
        Message message = messageService.getOne(Wrappers.<Message>query()
                .eq("user_id", LoginUtil.getLoginUser().getId())
                .ge("exp_time", DateUtil.date().toDateStr())
                .orderByDesc("create_time").last("limit 1"));

        return Result.ok(message);
    }


    /**
     * 删除消息
     * @param id
     * @return
     */
    @PostMapping("/manage/delete")
    @ResponseBody
    public Result delete(@RequestBody List<String> id){
        messageService.removeByIds(id);
        return Result.ok();
    }
}
